/**
 * 用户管理
 * @author YANGSHT
 */
 //创建目录管理模块
var catalogManagerApp = angular.module("catalogManagerApp", [ "ngSanitize", "ui.select", "commonApp", "sysServiceApp", "ngDropTree", "ngJsTree"]);
 //创建目录管理控制器
catalogManagerApp.controller("catalogManagerCtrl", [
		"$scope",
		"commonService", 
		"sysService",
		"$timeout",
		"$log",
	 	function($scope, commonService, sysService, $timeout, $log){

				// 查询条件
				$scope.queryParams = {
					orgID : null,
					userName : null
				};

				// 当前部门对象
				$scope.currentOrg = {
					id : null,
					orgName : null,
					orgType : null,
					orgParent : null,
					queryAll : false
				};

				// 添加部门对象
				$scope.submitOrg = {};

				// 添加用户对象
				$scope.submitUser = {
					userName : null,
					userCode : null,
					password : null,
					phone : null,
					rtx : null,
					orgId : null,
					orgName : null
				};
				
				// 按钮操作控制
				$scope.action = null;
				$scope.$orgNameLabel = $("#orgNameLabel");

				// 模态框对象
				$scope.$userOrgEditModal = $("#userOrgEditModal");
				$scope.$userOrgSelectModal = $("#userOrgSelectModal");
				$scope.$userTableEditModal = $("#userTableEditModal");
				
				// 表格对象
				$scope.$userTable = $('#userTable');

				// 按钮对象
				$scope.$addUserButton = $("#addUserButton");
				$scope.$modUserButton = $("#modUserButton");
				$scope.$removeUserButton = $("#removeUserButton");
				$scope.$addUserOrgButton = $("#addUserOrgButton");
				$scope.$removeUserOrgButton = $("#removeUserOrgButton");
				

				// 表格选中实例对象
				$scope.selections = [];
				$scope.selectionIds = [];

				// 监视isSucce的状态
				$scope.isSuccess = true;
				$scope.error = null;
				
				ffc.util.watchResult($scope, $timeout);	
				
					
			    /**
			     * 
			     * 功能说明:部门树配置
			     * 
			     * @author ZHONGFUHUA
			     *
			     * @Date 2016年7月28日
			     *
			     */
				$scope.treeConfig = {
						config : {
						},
						types : {
							'default' : {
								icon : "glyphicon glyphicon-folder-open"
							},
							'catalog' : {
								icon : "glyphicon glyphicon-file"
							}
						},
						events : {
							selectNode : function(e, data) {
								if($scope.action == null){
									$scope.queryParams.orgID = data.selected[0];
								}
								$scope.currentOrg.id = data.node.id;
								$scope.submitOrg.orgParent = data.node.id;
								$scope.currentOrg.orgName = data.node.text;
								$scope.currentOrg.orgParent = data.node.parent;
								
								$scope.submitUser.orgId = data.selected[0];
								$scope.submitUser.orgName = data.node.text;
								
								$scope.$removeUserOrgButton.prop("disabled", false);
								$scope.$addUserButton.prop("disabled", false);
								$scope.$userOrgSelectModal.modal("hide");
								$scope.$userTable.bootstrapTable("selectPage", 1);
							}
						},
						icons : true,
						multiple : false,
						animation : 200,
						status : '',
						search : "",
						selected : [],
						plugins : [],
						data : []
				}
				
				
					 
			    /**
			     * 
			     * 功能说明:初始化部门树
			     * 
			     * @author ZHONGFUHUA
			     *
			     * @Date 2016年7月28日
			     *
			     */
				 $scope.initOrgTree=function(){
					   sysService.queryOrgTree({"children": []}, function(data) {
			               if (data.result) {
			            	   $scope.treeConfig.data = data.dataList;
			               }
			           });
				 };

					  
				/**
				 * 
				 * 功能说明:每次选择时重新获取表格中被选中对象
				 * 
				 * @author ZHONGFUHUA
				 * 
				 * @Date 2016年10月25日
				 * 
				 */
				$scope.$userTable.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
					$scope.$removeUserButton.prop('disabled', !$scope.$userTable.bootstrapTable('getSelections').length);
					$scope.$modUserButton.prop('disabled', !$scope.$userTable.bootstrapTable('getSelections').length);
					$scope.getSelections();
				})
					
				
				/**
				 * 
				 * 功能说明:每次选择时重新获取表格中被选中对象
				 * 
				 * @author ZHONGFUHUA
				 * 
				 * @Date 2016年10月25日
				 * 
				 */
				$scope.getSelections = function() {
					$scope.selections=[];
					$scope.selectionIds=[];
					return $.map($scope.$userTable.bootstrapTable('getSelections'), function (row) {
						$scope.selectionIds.push(row.id);
						$scope.selections.push(row);
						return row;
					});
				}
				
				
				
			    /**
			     * 
			     * 功能说明:根据姓名查询用户信息
			     * 
			     * @author ZHONGFUHUA
			     *
			     * @Date 2016年7月28日
			     *
			     */
				$scope.queryUser=function(){
					$scope.$userTable.bootstrapTable("selectPage", 1);
				}
				
				/**
				 * 
				 * 功能说明:保存部门
				 * 
				 * @author ZHONGFUHUA
				 * 
				 * @Date 2016年10月25日
				 * 
				 */
				$scope.addOrg = function() {
					sysService.saveOrg($scope.submitOrg,
						function(result) {
							if (!result.result) {
								$scope.error = result.msgTitle;
								$scope.isSuccess = false;
								return false;
							}	
							new MessageDialog().success("保存成功");
							$scope.$userOrgEditModal.modal("hide");
							$scope.initOrgTree();
							$scope.queryUserByOrg();
						}, function(error) {
							$scope.error = error;
							$scope.isSuccess = false;
						}
					);
				}
				
				
				/**
				 * 
				 * 功能说明:删除部门
				 * 
				 * @author ZHONGFUHUA
				 * 
				 * @Date 2016年10月25日
				 * 
				 */
				$scope.deleteOrg = function(){
					new MessageDialog().deletion("确定删除"+$scope.currentOrg.orgName+"?",function(){
						sysService.deleteOrg({id:$scope.currentOrg.id},function(result){
							if(!result.result){
								$scope.error = result.msgTitle;
								$scope.isSuccess = false;
								return false;
							}
							$scope.initOrgTree();
							$scope.queryParams={};
							$scope.$table.bootstrapTable("selectPage", 1);
						},function(error){
							$scope.error = error;
							$scope.isSuccess = false;
						});
					});
				};
				
				
				/**
				 * 
				 * 功能说明:修改新增用户
				 * 
				 * @author ZHONGFUHUA
				 * 
				 * @Date 2016年10月25日
				 * 
				 */
				$scope.saveUser=function(){
					if($scope.action=="MOD_USER"){
						sysService.updateUser($scope.submitUser,
							function(result){
								if(!result.result){
									$scope.error = result.msgTitle;
									$scope.isSuccess=false;
									return false;
								}
								$scope.$userTableEditModal.modal("hide");
								$scope.$userTable.bootstrapTable("selectPage", 1);
								new MessageDialog().success("修改成功!!");
							},function(error){
								$scope.error = error;
								$scope.isSuccess = false;
							}
						);
					}else{
						sysService.saveUser($scope.submitUser,
							function(result){
								if(!result.result){
									$scope.error = result.msgTitle;
									$scope.isSuccess = false;
									return false;
								}
								$scope.$userTableEditModal.modal("hide");
								$scope.$userTable.bootstrapTable("selectPage", 1);
								new MessageDialog().success("添加成功!!");
							},function(error){
								$scope.error = error;
								$scope.isSuccess = false;
							}
						);
					}
				}
				
				
				/**
				 * 
				 * 功能说明:删除用户
				 * 
				 * @author ZHONGFUHUA
				 * 
				 * @Date 2016年10月25日
				 * 
				 */
				$scope.deleteUser = function () {
					  new MessageDialog().deletion("确定删除用户？", function() {
							sysService.deleteUsers($scope.selections,
								function(result) {
									if (!result.result) {
										$scope.error = result.msgTitle;
										$scope.isSuccess = false;
										return false;
									}
									$scope.$userTable.bootstrapTable("selectPage", 1);
									$scope.$removeUserButton.prop('disabled', true);
									$scope.$modUserButton.prop('disabled', true);
								},function(error) {
									$scope.error = error;
									$scope.isSuccess = false;
								}
							);
					  });
				};

				
				/**
				 * 
				 * 功能说明:弹出框打开事件
				 * 
				 * @author ZHONGFUHUA
				 * 
				 * @Date 2016年10月25日
				 * 
				 */
				$scope.$userTableEditModal.on('shown.bs.modal',function(){
					if($scope.action=="MOD_USER"){
						$scope.submitUser = angular.copy($scope.selections[0]);
						ffc.util.angularApply($scope, $timeout);;
					}else{
						$scope.submitUser.userName=null;
						$scope.submitUser.userCode=null;
						$scope.submitUser.password=null;
						$scope.submitUser.phone=null;
						$scope.submitUser.rtx=null;
					}
				});
				
				
				/**
				 * 
				 * 功能说明:弹出框关闭事件
				 * 
				 * @author ZHONGFUHUA
				 * 
				 * @Date 2016年10月25日
				 * 
				 */
				$scope.$userTableEditModal.on("hidden.bs.modal",function(){
					$scope.action = null;
				});
				
					
				$(function() {
						$scope.initOrgTree();
						$scope.$userTable.bootstrapTable({
							queryParams: function(params) {
								var selfParams = {
										"children": []
								};
								if ($scope.queryParams.orgID != null) {
									selfParams.children.push({"name":"orgId", "value":$scope.queryParams.orgID});
								}
								if ($scope.queryParams.userName != null) {
									selfParams.children.push({"name":"userName", "operator":"like", "value":$scope.queryParams.userName+"%"});
								}
								return $.extend({}, params, selfParams);
							},
							ajax : function(render) {
									if (!$scope.currentOrg.id&&!$scope.queryParams.userName) {
										ffc.util.loadTableData(render);
									} else {
										sysService.queryUserDataByMap(render.data, 
											function(result) {
												if(!result.result){
													$scope.error = result.msgTitle;
													$scope.isSuccess = false;
													return;
												}
												ffc.util.loadTableData(render, result);
											},function(error){
												$scope.error = error;
												$scope.isSuccess = false;
											}
										);
									}
									$scope.$removeUserButton.prop('disabled', true);
									$scope.$modUserButton.prop('disabled', true);
							}
					});
					
				});
				
 }])
